package com.hsytech.baoinbao;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;

/**
 * Created by hsy on 2016/3/17.
 */
public abstract class GroupedGridViewAdapter extends BaseAdapter {
    public abstract int getGroupCount();

    public abstract Object getGroupItem(int groupPosition);

    public abstract long getGroupItemId(int groupPosition);

    public abstract View getGroupView(int groupPosition, View convertView);

    public abstract int getGridNumColumns(int groupPosition);

    public abstract int getGridCount(int groupPosition);

    public abstract Object getGridItem(int groupPosition, int gridPosition);

    public abstract long getGridItemId(int groupPosition, int gridPosition);

    public abstract View getGridItemView(int groupPosition, int gridPosition, View convertView);

    public abstract void onGroupClick(int groupPosition, View v);

    public abstract void onGridItemClick(int groupPosition, int gridPosition, View v);

    public GridView getGridView(int groupPosition) {
        GridView gridView = new FullDrawGridView(mContext);
        gridView.setAdapter(new GridAdapter(this, groupPosition));
        gridView.setNumColumns(getGridNumColumns(groupPosition));
        gridView.setCacheColorHint(Color.TRANSPARENT);
        gridView.setVerticalSpacing(10);
        gridView.setHorizontalSpacing(10);
        return gridView;
    }

    private Context mContext;

    private View.OnClickListener mOnGroupClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onGroupClick((Integer) v.getTag(), v);
        }
    };

    public GroupedGridViewAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public final int getCount() {
        return getGroupCount() * 2;
    }

    @Override
    public final Object getItem(int position) {
        return getGroupItem(position);
    }

    @Override
    public final long getItemId(int position) {
        return getGroupItemId(position);
    }

    @Override
    public final View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (0 == position % 2) {
            view = getGroupView(position / 2, convertView);
            view.setTag(position / 2);
            view.setOnClickListener(mOnGroupClickListener);
            return view;
        } else {
            return getGridView((position - 1) / 2);
        }
    }

    private static class FullDrawGridView extends GridView {

        public FullDrawGridView(Context context) {
            super(context);
        }

        public FullDrawGridView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public FullDrawGridView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int h = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, h);
        }
    }

    private static class GridAdapter extends BaseAdapter {
        private int groupPosition;
        private GroupedGridViewAdapter container;
        private View.OnClickListener mOnGridClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                container.onGridItemClick(groupPosition, (Integer) v.getTag(), v);
            }
        };

        public GridAdapter(GroupedGridViewAdapter container, int groupPosition) {
            this.groupPosition = groupPosition;
            this.container = container;
        }

        @Override
        public int getCount() {
            return container.getGridCount(groupPosition);
        }

        @Override
        public Object getItem(int position) {
            return container.getGridItem(groupPosition, position);
        }

        @Override
        public long getItemId(int position) {
            return container.getGridItemId(groupPosition, position);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = container.getGridItemView(groupPosition, position, convertView);
            view.setTag(position);
            view.setOnClickListener(mOnGridClickListener);
            return view;
        }
    }
}


